package com.vainycos.lotterysearch.service.notify;

import cn.hutool.extra.cglib.CglibUtil;
import com.vainycos.lotterysearch.mapper.MyBigLotteryBallMapper;
import com.vainycos.lotterysearch.mapper.MyDoubleColorBallMapper;
import com.vainycos.lotterysearch.model.BigLotteryBall;
import com.vainycos.lotterysearch.model.DoubleColorBall;
import com.vainycos.lotterysearch.model.MyBigLotteryBall;
import com.vainycos.lotterysearch.model.MyDoubleColorBall;
import com.vainycos.lotterysearch.model.ro.BigLotteryRo;
import com.vainycos.lotterysearch.model.ro.DoubleColorRo;
import com.vainycos.lotterysearch.service.BigLotteryService;
import com.vainycos.lotterysearch.service.DoubleColorService;
import com.vainycos.lotterysearch.service.mail.MailTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Vainycos
 * @description
 * @date: 2022/11/10 17:08
 */
@Slf4j
@Service
public class MailNotify {

    @Autowired
    private MailTemplate mailTemplate;

    @Autowired
    private BigLotteryService bigLotteryService;

    @Autowired
    private DoubleColorService doubleColorService;

    @Resource
    private MyBigLotteryBallMapper myBigLotteryBallMapper;

    @Resource
    private MyDoubleColorBallMapper myDoubleColorBallMapper;

    /**
     *
     * @param updateBigLotteryBall
     * @param toWho
     */
    public void mailNotifyBigLotteryBall(List<BigLotteryBall> updateBigLotteryBall, List<String> toWho){
        updateBigLotteryBall.forEach(e->{
            List<MyBigLotteryBall> myBigLotteryBalls = myBigLotteryBallMapper.selectList(null);
            if (myBigLotteryBalls != null && myBigLotteryBalls.size() > 0){
                myBigLotteryBalls.forEach(m->{
                    BigLotteryRo copy = CglibUtil.copy(m, BigLotteryRo.class);
                    String subject = e.getLotteryDate() + "-" +e.getPeriodId() + "期" + "超级大乐透开奖";
                    log.info("开始发送邮箱->{}", subject);
                    mailTemplate.sendSimpleMessage(subject, bigLotteryService.describeByPrice(e, copy), toWho);
                });
            }

        });
    }

    /**
     *
     * @param updateDoubleColorBall
     * @param toWho
     */
    public void mailNotifyDoubleColorBall(List<DoubleColorBall> updateDoubleColorBall, List<String> toWho){
        updateDoubleColorBall.forEach(e->{
            List<MyDoubleColorBall> myDoubleColorBalls = myDoubleColorBallMapper.selectList(null);
            if (myDoubleColorBalls != null && myDoubleColorBalls.size() > 0){
                myDoubleColorBalls.forEach(m->{
                    DoubleColorRo copy = CglibUtil.copy(m, DoubleColorRo.class);
                    String subject = e.getLotteryDate() + "-" +e.getPeriodId() + "期" + "双色球开奖";
                    log.info("开始发送邮箱->{}", subject);
                    mailTemplate.sendSimpleMessage(subject, doubleColorService.describeByPrice(e, copy), toWho);
                });
            }
        });
    }
}
